#include "themesettings.h"
#include "ui_themesettings.h"
#include "graphicsplugin.h"
#include <Maestro/mcplugins.h>

QList<StyleInterface *> * ThemeSettings::stylePlugins;
//work around for issue 31
#if defined(Q_OS_LINUX)
    const QString beginning="#widget{ background-image: url(";
    const QString ending=")}";
#else
    const QString beginning="#widget{ border-width: 4px; border-image: url(";
    const QString ending=") 4 4 4 4 stretch stretch;}";
#endif
QWidget * ThemeSettings::widget;
QString ThemeSettings::fileName;

ThemeSettings::ThemeSettings(QWidget *parent) :
    SettingsMenuItem(parent),
    ui(new Ui::ThemeSettings)
{
    ui->setupUi(this);
    changeImage=new Button(QIcon(":/imgs/3F00Button_ChangeBackground.png"),"",this);
    changeImage->setIconSize(QSize(256,80));
    addRow(changeImage);
    QHBoxLayout * horizontalLayout=new QHBoxLayout();
    QList<Button *> * list=new QList<Button *>();
    saveButton = new Button(QIcon(":/imgs/3F00Button_Save.png"),"",this,NULL);
    saveButton->setIconSize(QSize(110,64));
    horizontalLayout->addWidget(saveButton);
    list->append(saveButton);
    cancelButton = new Button(QIcon(":/imgs/3F00Button_Cancel.png"),"",this);
    cancelButton->setIconSize(QSize(110,64));
    horizontalLayout->addWidget(cancelButton);
    list->append(cancelButton);
    addRow(list);
    ui->saveCancelWidget->setLayout(horizontalLayout);
}

ThemeSettings::~ThemeSettings()
{
    delete ui;
}

void ThemeSettings::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ThemeSettings::saveTest() {

}

void ThemeSettings::assertSaved() {

}

void ThemeSettings::cancelTest() {

}

void ThemeSettings::assertCancelled() {

}

void ThemeSettings::setStylePlugins(QList<StyleInterface *> *plugins) {
      stylePlugins=plugins;

}

void ThemeSettings::reshowComponents() {
    /*if(!themeSelector->hasChildren()&&stylePlugins) {
        defaultButton=new Button(QIcon(""),"Default",this);
        foreach(StyleInterface * interface,*stylePlugins) {
            Button * button=new Button(QIcon(""),interface->getName(),this,interface);
            themeSelector->addButton(button);
        }
    }*/
}

void ThemeSettings::buttonSelected(Button *button) {

}

void ThemeSettings::buttonSelected() {
    if(getCurrentButton()==saveButton) {
        save();
    } else if (getCurrentButton()==cancelButton) {
        cancel();
    } else if(getCurrentButton()==changeImage) {
        QDir directory=QDir(qApp->applicationDirPath());
        #if defined(Q_OS_WIN)
            if (directory.dirName().toLower() == "debug" || directory.dirName().toLower() == "release")
                directory.cdUp();
        #elif defined(Q_OS_MAC)
                directory.cdUp();
                directory.cdUp();
                directory.cdUp();
        #endif
        directory.cd("backgrounds");
        QString name=QFileDialog::getOpenFileName(this, tr("Open Image"), directory.path(), tr("Image Files (*.png *.jpg *.bmp)"));
        if(name.size()>0) {
            newFileName=name;
            widget->setStyleSheet(beginning+newFileName+ending);
        }
    }
}

void ThemeSettings::save() {
    fileName=newFileName;
    Querier::getResult("UPDATE MainSettings SET value='"+fileName+"' WHERE settingName='background'");
    emit completed();
}

void ThemeSettings::cancel() {
    widget->setStyleSheet(beginning+fileName+ending);
    newFileName=fileName;
    emit completed();
}

void ThemeSettings::setWidget(QWidget *w) {
    QSqlQueryModel * result=Querier::getModel("SELECT * FROM MainSettings WHERE settingName='background'");
    fileName=result->record(0).value("value").toString();
    qDebug()<<"file name is"<<fileName;
    widget=w;
    if(QImage().load(fileName)) {
       widget->setStyleSheet(beginning+fileName+ending);
    }
}
